package com.tb.inputmethod.pinyin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

public class MainActivity extends Activity implements View.OnClickListener {
    private EditText mEt;
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEt = findViewById(R.id.et_test);
        mTv = findViewById(R.id.tv_test);

        findViewById(R.id.btn_go_settings).setOnClickListener(this);
        findViewById(R.id.btn_select_keyboard).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_go_settings:

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS);
                startActivity(intent);
                break;

            case R.id.btn_select_keyboard:

//                getStatusBarHeight(getApplicationContext());
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
                break;
        }
    }


    //重写此方法在Activity中使用菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //第一个参数用于指定通过哪一个资源文件来创建菜单，第二个参数用于指定我们的菜单项将添加到哪一个Menu对象中
        getMenuInflater().inflate(R.menu.main_menu, menu);
        //返回true表示允许显示创建的菜单
        return true;

    }

    //重写此方法来定义菜单的响应事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //通过item.getItemId()来判断我们点击的是哪一个菜单项
        switch (item.getItemId()) {
            case R.id.setting:
                startActivity(new Intent(this, SettingsActivity.class));
                break;
        }
        return true;
    }

    /**
     * 测试API
     * @param view
     */
    public void test(View view) {
        //获取输入字母
        String test = mEt.getText().toString().trim();
        // 最大显示
        int MAX_SHOW = 50;

        //获取到待选列表长度
        int length_choice = PinyinDecoderService.nativeImSearch(test.getBytes(), test.length());

        //生成待选
        Vector<String> choiceList = new Vector<String>();
        for (int i = 0; i < (length_choice>MAX_SHOW?MAX_SHOW:length_choice); i++) {
            String retStr = PinyinDecoderService.nativeImGetChoice(i);
            if (0 == i) retStr = retStr.substring(0);
            choiceList.add(retStr);
        }

        String test_PreDict = "";
        //选中为第一个，生成对应联想词
        if(choiceList.size() >0){
            test_PreDict = choiceList.get(0);
        }

        //获取联想集合长度
        int length_preDict = PinyinDecoderService.nativeImGetPredictsNum(test_PreDict);

        Vector<String> predictList = new Vector<String>();
        for (int i = 0; i < (length_preDict>MAX_SHOW?MAX_SHOW:length_preDict) ; i++) {
            String item = PinyinDecoderService.nativeImGetPredictItem(i);
            predictList.add(item);
        }

        //显示
        mTv.setText(
                ">>1.获取待选:\n"+ Arrays.toString(choiceList.toArray())+
                "\n\n>>2:获取联想词(" +test_PreDict+"):\n"+Arrays.toString(predictList.toArray()));
    }
}